/**
 * created by dt at 2025/5/6
 */

import { System } from '../../core/System';
import { ecs_trait_property } from '../../core/Trait';
import { ecs_trait } from '../../core/Trait';
import { ECBuffManager } from '../components/ECBuffManager';

@ecs_trait()
class BuffTrait {
    @ecs_trait_property(ECBuffManager)
    buffManager!: ECBuffManager;
}

export class BuffSystem extends System {
    public onStart(): void {
        // 系统启动时的初始化
    }

    public onUpdate(deltaTime: number): void {
        this.world.forEach(BuffTrait, (entity, bundle) => {
            this.updateBuffs(bundle.buffManager, deltaTime);
        });
    }
    private updateBuffs(buffManager: ECBuffManager, deltaTime: number): void {
        // 获取所有需要更新的buff
        const buffsToUpdate = buffManager.buffs;
        
        // 更新每个buff
        for (const [id, buff] of buffsToUpdate) {
            // 更新buff时间
            buff.update(deltaTime);
            
            // 如果buff已失效，移除它
            if (!buff.isActive) {
                buffManager.buffs.delete(id);
            }
        }
    }
} 